#!/usr/bin/env python
# coding=utf-8

import os
import sys

f = open("svnlist.txt")

svn_root_url = "https://svn.baidu.com/"

lists = f.readlines()
for line in lists:
    line = line.strip("\n")
    if (line.startswith("#")):
        print "skip : " + line
        continue

    tmp = line.split(" ")
    if (len(tmp) < 2):
    	print "error line : " + line
    	continue

    module_name = tmp[0]
    svn_path = tmp[1]
    svn_path = svn_path.strip("/")
    svn_full_url = svn_root_url + svn_path

    # get svn path split
    if (svn_path.find("/trunk/") >= 0): # trunk
        trunk = True
    else:
        trunk = False

    if (trunk):
        src_dir = svn_path
    else:
        tmp = svn_path.split("/")
        svn_branch = tmp[-1]
        svn_module_name = tmp[-2]
        svn_module_base = "/".join(tmp[0:-2])

        # dir to co source
        src_dir = svn_module_base + "/" + module_name + "_" + svn_branch

    # mkdir
    cmd = "mkdir -p " + src_dir
    ret = os.system(cmd)
    if (ret != 0):
        print "[excute failed] " + cmd
        exit(-1)
    else:
        print "[excute successful] " + cmd

    # checkout
    cmd = "svn checkout " + svn_full_url + " " + src_dir + " > /dev/null"
    ret = os.system(cmd)
    if (ret != 0):
        print "[excute failed] " + cmd
        exit(-1)
    else:
        print "[excute successful] " + cmd

    if (trunk): # make a copy
        print "[info] path is on the trunk, make a copy to branch"
        src_dir = src_dir.replace("/trunk/", "/branches/");
        src_dir = "/".join(src_dir.split("/")[0:-1]) + "/" + src_dir.split("/")[-1] + "_trunk"
        cmd = "mkdir -p " + src_dir
        ret = os.system(cmd)
        if (ret != 0):
            print "[excute failed] " + cmd
            exit(-1)
        else:
            print "[excute successful] " + cmd
        cmd = "svn checkout " + svn_full_url + " " + src_dir + " > /dev/null"
        ret = os.system(cmd)
        if (ret != 0):
            print "[excute failed] " + cmd
            exit(-1)
        else:
            print "[excute successful] " + cmd


